package com.guie.kjzh.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import me.zhengjie.base.PageInfo;
import me.zhengjie.base.QueryHelpMybatisPlus;
import me.zhengjie.base.impl.CommonServiceImpl;
import me.zhengjie.modules.system.service.DeptService;
import me.zhengjie.modules.system.service.dto.*;
import me.zhengjie.utils.ConvertUtil;
import me.zhengjie.utils.PageUtil;
import com.guie.kjzh.domain.Pay;
import com.guie.kjzh.service.PayService;
import com.guie.kjzh.service.dto.PayDto;
import com.guie.kjzh.service.dto.PayQueryParam;
import com.guie.kjzh.service.mapper.PayMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* @author simon
* @date 2022-02-24
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = PayService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class PayServiceImpl extends CommonServiceImpl<PayMapper, Pay> implements PayService {

    // private final RedisUtils redisUtils;
    private final PayMapper payMapper;
    private final DeptService deptService;

    @Override
    public PageInfo<PayDto> queryAll(PayQueryParam query, Pageable pageable) {
        IPage<Pay> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<Pay> page = payMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        List<PayDto> payDtos = ConvertUtil.convertList(page.getRecords(), PayDto.class);
        if (page.getTotal() > 0) {
            Map<Long, DeptDto> deptMap = deptService.queryAll().parallelStream()
                                                    .collect( Collectors.toMap(DeptDto::getId, Function.identity(), ( x, y) -> x));

            payDtos.forEach(pay -> {
                DeptDto deptDto = deptMap.get(pay.getDeptId());
                if ( deptDto != null )
                {
                    pay.setDeptName(deptDto.getName());
                }
            });
        }

        return new PageInfo<>(page.getTotal(), payDtos);
    }

    @Override
    public List<PayDto> queryAll(PayQueryParam query){
        return ConvertUtil.convertList(payMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), PayDto.class);
    }

    @Override
    public Pay getById(Long id) {
        return payMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public PayDto findById(Long id) {
        return ConvertUtil.convert(getById(id), PayDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(PayDto resources) {
        Pay entity = ConvertUtil.convert(resources, Pay.class);
        return payMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(PayDto resources){
        Pay entity = ConvertUtil.convert(resources, Pay.class);
        int ret = payMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Long> ids){
        // delCaches(ids);
        return payMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Long id){
        Set<Long> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Long id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Long> ids) {
        for (Long id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<PayDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (PayDto pay : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("部门名称", pay.getDeptId());
              map.put("用户名", pay.getName());
              map.put("昵称", pay.getNick());
              map.put("性别", pay.getGender());
              map.put("是否为admin账号", pay.getIsAdmin());
              map.put("状态：1启用、0禁用", pay.getEnabled());
              map.put("创建者", pay.getCreateBy());
              map.put("更新者", pay.getUpdateBy());
              map.put("创建日期", pay.getCreateTime());
              map.put("更新时间", pay.getUpdateTime());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
